package cn.db101.view;


import cn.db101.dao.bookDao;
import cn.db101.model.book;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class reader_borrow_mainFrame {
	JFrame jf = new JFrame();
	private JPanel contentPane;
	private JTextField bookid_textField;
	private JTextField bookname_textField;
	private JTextField bookstatus_textField;
	private JTextField readerid_textField;


	/**
	 * Launch the application.
	 * public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainFrame frame = new mainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	 */
	

	/**
	 * Create the frame.
	 */
	public  void init() {
		jf.setTitle("\u8BFB\u8005主界面");
		
		jf.setIconImage(Toolkit.getDefaultToolkit().getImage(reader_borrow_mainFrame.class.getResource("/icon/Libraries_64px_1176502_easyicon.net.png")));
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setBounds(100, 100, 1278, 805);
		
		JMenuBar menuBar = new JMenuBar();
		jf.setJMenuBar(menuBar);
		
		JMenu m1 = new JMenu("主界面");
		m1.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/线性图书.png")));
		menuBar.add(m1);
		
		JMenu m2 = new JMenu("图书管理规定");
		m2.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/线性老师.png")));
	
		
		menuBar.add(m2);
		
		JMenu m3 = new JMenu(" 图书查询");
		m3.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/线性本子vg.png")));
		menuBar.add(m3);
		
		JMenu m4 = new JMenu("图书借还");
		m4.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/线性铅笔.png")));
		menuBar.add(m4);
		
		JMenu m5 = new JMenu("账户管理");
		m5.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/线性双指点击.png")));
		menuBar.add(m5);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		jf.setContentPane(contentPane);
		
		JLayeredPane layeredPane = new JLayeredPane();
		contentPane.add(layeredPane, BorderLayout.CENTER);
		
		JLayeredPane layeredPane_1 = new JLayeredPane();
		layeredPane_1.setBounds(405, 212, 523, 313);
		layeredPane.add(layeredPane_1);
		
		JLabel booknum_label = new JLabel("\u8BF7\u8F93\u5165\u4E66\u53F7\uFF1A");
		booknum_label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		booknum_label.setBounds(59, 48, 116, 18);
		layeredPane_1.add(booknum_label);
		
		bookid_textField = new JTextField();
		bookid_textField.setColumns(10);
		bookid_textField.setBounds(185, 49, 120, 24);
		layeredPane_1.add(bookid_textField);
		
		JButton search_Button = new JButton("\u641C\u7D22");
		search_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				searchBook(e);
				
			}
		});
		search_Button.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/\u5E95\u680F-\u6570\u636E\u67E5\u8BE21.png")));
		search_Button.setForeground(Color.BLACK);
		search_Button.setBackground(Color.WHITE);
		search_Button.setBounds(343, 46, 113, 27);
		layeredPane_1.add(search_Button);
		
		JLabel bookname_label_1 = new JLabel("书名：");
		bookname_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		bookname_label_1.setBounds(161, 114, 70, 18);
		layeredPane_1.add(bookname_label_1);
		
		JLabel author_label_1_1 = new JLabel("\u4F5C\u8005\uFF1A");
		author_label_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		author_label_1_1.setBounds(161, 164, 70, 18);
		layeredPane_1.add(author_label_1_1);
		
		JLabel state_label_1_2 = new JLabel("\u72B6\u6001\uFF1A");
		state_label_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		state_label_1_2.setBounds(161, 216, 70, 18);
		layeredPane_1.add(state_label_1_2);
		
		JButton return_Button = new JButton("\u8FD8\u4E66");
		return_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				returnBook(e);
			}
		});
		return_Button.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/线性书写.png")));
		return_Button.setForeground(Color.BLACK);
		return_Button.setBackground(Color.WHITE);
		return_Button.setBounds(84, 286, 113, 27);
		layeredPane_1.add(return_Button);
		
		JButton borrow_Button = new JButton("借阅");
		borrow_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowBook(e);
			}
		});
		borrow_Button.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/icon/线性书架.png")));
		borrow_Button.setForeground(Color.BLACK);
		borrow_Button.setBackground(Color.WHITE);
		borrow_Button.setBounds(288, 286, 113, 27);
		layeredPane_1.add(borrow_Button);
	
		bookname_textField = new JTextField();
		bookname_textField.setColumns(10);
		bookname_textField.setBounds(228, 99, 120, 24);
		layeredPane_1.add(bookname_textField);
		
		bookstatus_textField = new JTextField();
		bookstatus_textField.setColumns(10);
		bookstatus_textField.setBounds(228, 150, 120, 24);
		layeredPane_1.add(bookstatus_textField);
		
		readerid_textField = new JTextField();
		readerid_textField.setColumns(10);
		readerid_textField.setBounds(228, 198, 120, 24);
		layeredPane_1.add(readerid_textField);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(reader_borrow_mainFrame.class.getResource("/img/borrow.jpg")));
		label.setBounds(0, 0, 1250, 722);
		layeredPane.add(label);
		
		m1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new reader_mainFrame().init();
			}
		});	
		m2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new reader_regulationFrame().init();
			}
		});	
		m3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new reader_searchFrame().init();
			}
		});	
		m5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new reader_account_mainFrame().init();
			}
		});
		//���ô���ɼ�
		jf.setVisible(true);	
		//����JFrame������ʾ
		jf.setLocationRelativeTo(null);
	}
	protected void searchBook(ActionEvent e) {
		// TODO Auto-generated method stub
		
		String book_id = bookid_textField.getText();
		if (book_id == null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}else {
			
			book newbook = new book();
			newbook = new bookDao().bookInfo(book_id);
			if(newbook != null) {
				bookname_textField.setText(newbook.getBook_name());
				if (newbook.getBook_status() == 1) {
					bookstatus_textField.setText("�ѽ��");
				}else {
					bookstatus_textField.setText("������");
				}
			}else {
				JOptionPane.showMessageDialog(null, "δ�ҵ���ͼ��");
				bookname_textField.setText("");
				bookstatus_textField.setText("");
				readerid_textField.setText("");
				return;
			}
		}
	}

	public void closeThis() {
		jf.dispose();
	}
	
	protected void borrowBook(ActionEvent e) {
		
		String book_id = bookid_textField.getText();
		String reader_id = readerid_textField.getText();
		if (book_id == null || book_id == "") {
			JOptionPane.showMessageDialog(null, "ͼ��id����Ϊ��");
			return;
		}
		if(reader_id == null || reader_id == "") {
			JOptionPane.showMessageDialog(null, "����id����Ϊ��");
			return;
		}
		int result = new bookDao().borrowBookDao(book_id, reader_id);
		if(result == 1) {
			JOptionPane.showMessageDialog(null, "���ĳɹ�");
			return;
		}else if(result == 0){
			JOptionPane.showMessageDialog(null, "����ʧ��");
			return;
		}else if(result == -1) {
			JOptionPane.showMessageDialog(null, "���鲻�ڿ���");
			return;
		}else if(result == -2) {
			JOptionPane.showMessageDialog(null, "�˶��߲��ɽ���");
			return;
		}
		
		
	}
	
	protected void returnBook(ActionEvent e) {
		
		String book_id = bookid_textField.getText();
		String reader_id = readerid_textField.getText();
		if (book_id == null || book_id == "") {
			JOptionPane.showMessageDialog(null, "ͼ��id����Ϊ��");
			return;
		}
		if(reader_id == null || reader_id == "") {
			JOptionPane.showMessageDialog(null, "����id����Ϊ��");
			return;
		}
		int result = new bookDao().returnBookDao(book_id, reader_id);
		if(result == 1) {
			JOptionPane.showMessageDialog(null, "����ɹ�");
			return;
		}else if(result == 0){
			JOptionPane.showMessageDialog(null, "����ʧ��");
			return;
		}else if(result == -1) {
			JOptionPane.showMessageDialog(null, "����δ�����");
			return;
		}else if(result == -2) {
			JOptionPane.showMessageDialog(null, "�˶���δ����򲻴���");
			return;
		}
		
	}

	public static void main(String[] args) {//�ͻ������
		new reader_borrow_mainFrame().init();	
	}
}
